package com.hxzy.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.ExistsValue;
import com.hxzy.common.annon.StopWatch;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import com.hxzy.controller.BaseController;
import com.hxzy.dto.SysRoleSaveDTO;
import com.hxzy.dto.SysRoleSearchDTO;
import com.hxzy.entity.SysRole;
import com.hxzy.service.ExistsValueService;
import com.hxzy.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * RestFul风格编写中， 接口具有幂等性的方式： GET/PUT(update)/DELETE    不管你访问多少次接口，它的结果是一致的 1^n=1
 *                     接口不具有幂等性 ： Post：Insert（增加资源）
 */
@Api(tags = "角色API")
@RestController
@RequestMapping(value = "/api/role")
public class SysRoleController  extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private ExistsValueService existsValueService;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R search(SysRoleSearchDTO dto){
        IPage<SysRole> page=this.sysRoleService.search(dto);
        return super.ipageToPage(page);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{roleId}")
    public R findById(@PathVariable(value = "roleId") BigDecimal roleId){
        SysRole sysRole = this.sysRoleService.getById(roleId);
        if(sysRole==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        sysRole.setUpdateBy(null);
        sysRole.setUpdateTime(null);
        sysRole.setCreateBy(null);
        sysRole.setCreateTime(null);

        return R.okHasData(sysRole);
    }

    @ApiOperation(value = "查询该角色拥有的菜单ID")
    @GetMapping(value = "/ownmenu/{roleId}")
    public R getRoleOwnMenuId(@PathVariable(value = "roleId") BigDecimal roleId){
        List<BigDecimal>  listMenuId=this.sysRoleService.getRoleOwnMenuId(roleId);
        return R.okHasData(listMenuId);
    }


    @ApiOperation(value = "新增角色")
    @PostMapping
    public  R add(@RequestBody @Valid SysRoleSaveDTO sysRoleSaveDTO){
        //验证账号唯一性
        ExistsValue numberValue=new ExistsValue();
        numberValue.setTableName("SYS_ROLE");
        numberValue.setColumnName("ROLE_NAME");
        numberValue.setColumnValue(sysRoleSaveDTO.getRoleName());
        boolean numberCount=this.existsValueService.searchCount(numberValue);
        if(numberCount){
            R<Object> build = R.build(AckCode.FAIL);
            build.setMsg("角色名称已被占用");
            return build;
        }

        //验证权限标识唯一性
        ExistsValue numberKey=new ExistsValue();
        numberKey.setTableName("SYS_ROLE");
        numberKey.setColumnName("ROLE_KEY");
        numberKey.setColumnValue(sysRoleSaveDTO.getRoleKey());
        numberCount=this.existsValueService.searchCount(numberKey);
        if(numberCount){
            R<Object> build = R.build(AckCode.FAIL);
            build.setMsg("权限标识已被占用");
            return build;
        }

        boolean result=this.sysRoleService.add(sysRoleSaveDTO);
        return super.toAjax(result);
    }

    @StopWatch
    @ApiOperation(value = "修改角色")
    @PutMapping
    public  R update(@RequestBody @Valid SysRoleSaveDTO sysRoleSaveDTO){
        //验证账号唯一性
        ExistsValue numberValue=new ExistsValue();
        numberValue.setTableName("SYS_ROLE");
        numberValue.setPrimaryKeyName("ROLE_ID");
        numberValue.setPrimaryKeyValue(sysRoleSaveDTO.getRoleId());

        numberValue.setColumnName("ROLE_NAME");
        numberValue.setColumnValue(sysRoleSaveDTO.getRoleName());
        boolean numberCount=this.existsValueService.searchCount(numberValue);
        if(numberCount){
            R<Object> build = R.build(AckCode.FAIL);
            build.setMsg("账号已被占用");
            return build;
        }

        //验证权限标识唯一性
        ExistsValue numberKey=new ExistsValue();
        numberKey.setTableName("SYS_ROLE");
        numberKey.setPrimaryKeyName("ROLE_ID");
        numberKey.setPrimaryKeyValue(sysRoleSaveDTO.getRoleId());

        numberKey.setColumnName("ROLE_KEY");
        numberKey.setColumnValue(sysRoleSaveDTO.getRoleKey());
        numberCount=this.existsValueService.searchCount(numberKey);
        if(numberCount){
            R<Object> build = R.build(AckCode.FAIL);
            build.setMsg("权限标识已被占用");
            return build;
        }

        boolean result=this.sysRoleService.edit(sysRoleSaveDTO);
        return super.toAjax(result);
    }


}
